package com.demo.common;

import com.demo.enums.RestFulStatus;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;

import java.io.Serializable;

/**
 *
 */
@Data
public class Result implements Serializable {

	private boolean success = true;
	private String message = "操作成功！";
	private Integer code = 0;
	private Object result;
	private long timestamp = System.currentTimeMillis();
	Result(){
		this.message = "操作成功";
		this.code = 200;
	}
	public Result(Throwable e){
		this.message = e.getMessage();
	}
	public Result(String msg){
		this.message = msg;
		this.code = 200;
	}
	public Result(Integer code,String msg){
		this.message = msg;
		this.code = code;
	}
	public Result success(Object result) {
		this.code = 200;
		this.success = true;
		this.result = result;
		return this;
	}
	public static Result ok() {
		Result result = new Result();
		return result;
	}
	public static Result ok(String msg) {
		Result result = new Result(200,msg);
		return result;
	}

	public static Result error(){
		Result result = new Result(500,"操作失败");
		return result;
	}
	public static Result error(String msg){
		Result result = new Result(500,msg);
		return result;
	}

	public  static Result error(Integer code, String msg){
		Result result = new Result(code,msg);
		return result;
	}

	public static Result error(RestFulStatus status) {
		return error(status.value(), status.getReasonPhrase());
	}


}
